package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.ACM;
import com.example.demo.mapper.ACMMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/ACM")
public class ACMController {

    @Resource
    ACMMapper ACMMapper;

    @PostMapping  //增
    public Result<?> save(@RequestBody ACM ACM) {
        ACMMapper.insert(ACM);
        return Result.success();
    }

    @DeleteMapping("/{id}") //删
    public Result<?> delete(@PathVariable long id) {
        ACMMapper.deleteById(id);
        return Result.success();
    }

//    @GetMapping //查
//    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
//                              @RequestParam(defaultValue = "20") Integer pageSize,
//                              @RequestParam(defaultValue = "") String search) {
//
//        LambdaQueryWrapper<ACM> wrapper = Wrappers.<ACM>lambdaQuery();
//        if(StrUtil.isNotBlank(search)) {
//            wrapper.like(ACM::getTitle,search);
//        }
//        Page<ACM> ACMPage = ACMMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
//        return Result.success(ACMPage);
//    }

    @GetMapping //查
    public Result<?> GetLevelSort(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "100") Integer pageSize,
                              @RequestParam(defaultValue = "",required = false) String Level) {

        LambdaQueryWrapper<ACM> wrapper = Wrappers.<ACM>lambdaQuery();
        if(StrUtil.isNotBlank(Level)) {
            wrapper.like(ACM::getLevel,Level);
            wrapper.orderByDesc(ACM::getPassRatio);
        }
        Page<ACM> ACMPage = ACMMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(ACMPage);
    }

    @PutMapping //改
    public Result<?> update(@RequestBody ACM ACM) {
        ACMMapper.updateById(ACM);
        return Result.success();
    }
}
